<template>
  <el-container class="box">
    <el-aside :width="isCollapse?'64px':'300px'">
      <div class="logo">
        <img src="@/assets/img/logo.png" />
        <span>商城管理系统</span>
      </div>
      <el-menu
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b"
        :router="true"
        :default-active="$route.path"
        :collapse="isCollapse"
        :collapse-transition="false"
      >
        <el-submenu v-for="pm in menus" :key="pm.id" :index="pm.id + ''">
          <template #title>
            <i class="el-icon-location"></i>
            <span>{{ pm.authName }}</span>
          </template>
          <el-menu-item
            v-for="cm in pm.children"
            :key="cm.id"
            :index="'/home/' + cm.path"
          >
            <i class="el-icon-location"></i>
            <span>{{ cm.authName }}</span>
          </el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>
    <el-container class="main">
      <el-header height="60px">
        <el-button size="medium" icon="el-icon-s-unfold" @click=" isCollapse=!isCollapse "></el-button>
        <el-dropdown>
          <span class="user el-dropdown-link">
            <el-avatar :size="40">
              <img src="../assets/img/user.jpg" />
            </el-avatar>
            <span>admin</span>
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import _ from 'lodash'

export default {
  data() {
    return {
      menus: [],
      icons: {
        201: "el-icon-location",
      },
      isCollapse:false
    };
  },
  methods: {
    async initLeftMenu() {
      try {
        let menus = await this.$api.getLeftMenu();
        // console.log(menus);
        if (menus == "error") return;

        this.menus = menus;
      } catch (error) {
        console.log("HomeView-initLeftMenu-error:", error);
      }
    },
    resetAside(){
      let w = document.documentElement.clientWidth;
      this.isCollapse = w<=1300;
    },
  },
  created() {
    this.initLeftMenu();
  },
  mounted(){
    this.resetAside();
    // window.onresize = ()=>{
    //   this.resetAside();
    // }
    this.$resizeCallBack = _.debounce(()=>{
      console.log(1)
      this.resetAside();
    },300);
    window.addEventListener("resize", this.$resizeCallBack)
  },
  destroyed() {
    // window.onresize = undefined;
    window.removeEventListener("resize",this.$resizeCallBack)
  },
};
</script>


<style lang="less" scoped>
.box {
  height: 100%;
  background-color: @subbc;
  .logo {
    @h: 60px;
    height: @h;
    border-bottom: 2px solid #aaa;
    box-sizing: border-box;
    white-space: nowrap;
    img {
      height: (@h - 2);
      width: (@h - 2);
      padding: (@h / 10);
      box-sizing: border-box;
      vertical-align: middle;
      padding-left: 10px;
    }
    span {
      vertical-align: middle;
      font-size: 24px;
      padding-left: 10px;
    }
  }
}
.el-aside {
  background-color: #545c64;
  box-shadow: 0px 0px 12px #999;
  color: white;
  transition: all 0.2s ease-in;
}
.el-header {
  background-color: white;
  box-shadow: 0px 0px 12px #999;
}
.el-menu {
  border: unset;
}
.main {
  .el-header {
    background-color: white;
    box-shadow: 0px 0px 12px #333333;
    display: flex;
    align-items: center;
    justify-content: space-between;
    z-index: 10;
  }
  .user > * {
    vertical-align: middle;
    cursor: pointer;
    margin-left: 4px;
  }
}
</style>